<?php
class Audacious extends TemplateEngine_base
{
	
	private $userId = 1;
	
	public function home_action()
	{
		$bookmarks = DBUtils::instance()->selectToVOArray("Select b.url, u.bookmarkdate, b.bookmarkid from bookmarks b, userbookmarks u where b.bookmarkid = u.bookmarkid and u.userid = ? order by u.bookmarkdate desc", array($this->userId), true);
		
		$arr = array();
		foreach($bookmarks as $bookmark)
		{
			$tags = DBUtils::instance()->selectToVOArray("Select t.tagid, t.taglabel from bookmarktags bt, tags t where bt.tagid = t.tagid and bt.userid = ? and bt.bookmarkid = ?",
														 array($this->userId, $bookmark['BOOKMARKID']), true);
			
			$bookmark['TAGS'] = $tags;
			$arr[] = $bookmark;
		}
		
		$data = array();
		$data['bookmarks'] = $arr;
		//print_r($data);
		return $this->generatePage($data);
	}
	
	public function create_action()
	{
		$data = array();
		$data['formAction'] = 'doedit';
		return $this->generatePage($data);
	}
	
	/*public function docreate_action()
	{
		try
		{
			DBUtils::instance()->beginTrans();
			
			//Marque ta page
			$bookmarkId = $this->findOrCreateBookmark($_POST['url']);
			if(!$bookmarkId)
				throw new Exception();
				
			//Ajoute le bookmrk pour l'utilisateur
			$sql = "Insert into userbookmarks (userid, bookmarkid, bookmarkdate) values (?,?,now())";
			DBUtils::instance()->execute($sql, 
										 array($this->userId, $bookmarkId));
				
			//Insertion des tags
			$tags = explode(' ', $_POST['tags']);
			
			$sql = "Delete from bookmarktags where userid = ? and bookmarkid = ?";
			DBUtils::instance()->execute($sql, 
										 array($this->userId, $bookmarkId));
			foreach($tags as $t)
			{
				$tagId = $this->findOrCreateTag($t);
				if(!$tagId)
					continue;					 
				$sql = "Insert into bookmarktags (userid, bookmarkid, tagid) values (?,?,?)";
				DBUtils::instance()->execute($sql, 
											 array($this->userId, $bookmarkId, $tagId));
			}
			
			DBUtils::instance()->commitTrans();
		}
		catch(Exception $e)
		{
			DBUtils::instance()->rollbackTrans();
			throw $e;
		}
		
		$this->setTemplate('home');
		return $this->home_action();
	}*/
	
	private function findOrCreateBookmark($bookmark)
	{
		$bookmark = trim($bookmark);
		if(empty($bookmark))
			return null;
		
		$sql = "Select bookmarkid from bookmarks where upper(url) = upper(?)";
		$b = DBUtils::instance()->selectSingleVO($sql, array($bookmark));
		
		if($b)
		{
			return $b->BOOKMARKID;
		}
		else
		{
			$id = DBUtils::instance()->getId('bookmarks', 'id');
			$sql = "Insert into bookmarks (bookmarkid, url) values (?, ?)";
			DBUtils::instance()->execute($sql, array($id, $bookmark));
			
			return $id;
		}
	}
	
	private function findOrCreateTag($tag)
	{
		$tag = trim($tag);
		if(empty($tag))
			return null;
			
		$sql = "Select tagid from tags where upper(taglabel) = upper(?)";
		$t = DBUtils::instance()->selectSingleVO($sql, array($tag));
		
		if($t)
		{
			return $t->TAGID;
		}
		else
		{
			$id = DBUtils::instance()->getId('tags', 'id');
			$sql = "Insert into tags (tagid, taglabel) values (?, ?)";
						
			DBUtils::instance()->execute($sql, array($id, $tag));
			
			return $id;
		}
	}
	
	public function edit_action()
	{
		$bookmarkId = $_GET['id'];
		
		$bookmark = DBUtils::instance()->selectSingleVO("Select b.url, u.bookmarkdate, b.bookmarkid from bookmarks b, userbookmarks u where b.bookmarkid = u.bookmarkid and u.userid = ? and u.bookmarkid = ?", array($this->userId, $bookmarkId), true);
		
		if(!$bookmark)
			return;
		
		//ajoute les tags
		$tags = DBUtils::instance()->selectToVOArray("Select t.tagid, t.taglabel from bookmarktags bt, tags t where bt.tagid = t.tagid and bt.userid = ? and bt.bookmarkid = ?",
														 array($this->userId, $bookmarkId), true);
			
		$tag_arr = array();
		foreach($tags as $t)
			$tag_arr[] = $t['TAGLABEL'];
		
		$data = array();
		$data['formAction'] = 'doedit';
		$data['id'] = $bookmarkId;
		$data['bookmarkUrl'] = $bookmark['URL'];
		$data['tags'] = join(' ', $tag_arr);
		
		return $this->generatePageWithTemplate('create', $data);
	}
	
	public function doedit_action()
	{
		try
		{
			DBUtils::instance()->beginTrans();
			
			//Marque ta page
			$bookmarkId = $this->findOrCreateBookmark($_POST['url']);
			if(!$bookmarkId)
				throw new Exception();
				
			DBUtils::instance()->execute("Delete from userbookmarks where userid = ? and bookmarkid = ?",
										 array($this->userId,$bookmarkId ));
			
			$sql = "Insert into userbookmarks (userid, bookmarkid, bookmarkdate) values (?,?,now())";
			DBUtils::instance()->execute($sql, 
										 array($this->userId, $bookmarkId));
										 
			//Insertion des tags
			$tags = explode(' ', $_POST['tags']);
			
			$sql = "Delete from bookmarktags where userid = ? and bookmarkid = ?";
			DBUtils::instance()->execute($sql, 
											 array($this->userId, $bookmarkId));
			foreach($tags as $t)
			{
				$tagId = $this->findOrCreateTag($t);
				if(!$tagId)
					continue;					 
				$sql = "Insert into bookmarktags (userid, bookmarkid, tagid) values (?,?,?)";
				DBUtils::instance()->execute($sql, 
											 array($this->userId, $bookmarkId, $tagId));
			}
			
			DBUtils::instance()->commitTrans();
		}
		catch(Exception $e)
		{
			DBUtils::instance()->rollbackTrans();
			throw $e;
		}
		
		$this->setTemplate('home');
		return $this->home_action();
	}
	
	
}
?>